﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class LapHoaDonGiaoHangDAO
    {
        public static void Them(HoaDonGiaoHangDTO hoaDonGiaoHangDto, List<ChiTietHoaDonGiaoHangDTO> lstCTHoaDonDto)
        {
            //Tao va mo mot ket noi to co so du lieu
            using (OleDbConnection cnn = BoKetNoiCSDL.KetNoi())
            {
                //Tao doi tuong OleDbCommand tuong ung voi ket noi da mo
                using (OleDbCommand cmd = cnn.CreateCommand())
                {
                    //Khai bao mot transaction
                    OleDbTransaction transaction = cnn.BeginTransaction();
                    cmd.Transaction = transaction;
                    bool bInsertedHoaDonGH = false;
                    Int32 maHoaDonGH = new Int32();
                    foreach (ChiTietHoaDonGiaoHangDTO chiTiet in lstCTHoaDonDto)
                    {
                        int result = 0;
                        try
                        {
                            if (!bInsertedHoaDonGH)
                            {
                                cmd.CommandText = String.Format("Insert into HOA_DON_GIAO_HANG(NgayGiaoHang, BienSoXeGH, NgayLapHoaDon) values('{0}', '{1}', '{2}')", hoaDonGiaoHangDto.NgayGiaoHang.ToShortDateString(), hoaDonGiaoHangDto.BienSoXe, hoaDonGiaoHangDto.NgayLapHoaDon.ToShortDateString());
                                int effect = cmd.ExecuteNonQuery();
                                if (effect != 0)
                                {
                                    bInsertedHoaDonGH = !bInsertedHoaDonGH;
                                    cmd.CommandText = "Select @@Identity";
                                    maHoaDonGH = (int)cmd.ExecuteScalar();
                                }
                            }
                            string insertCmd = "Insert into CHI_TIET_HOA_DON_GIAO_HANG(MaHoaDonGH, MaCTHoaDonGH, MaDiaDiem, LoaiHang, SoLuong, MaTrangThai)";
                            insertCmd += "Values(?, ?, ?, ?, ?, ?)";
                            cmd.CommandText = insertCmd;


                            cmd.Parameters.Add("@MaHoaDonGH", OleDbType.Integer);
                            cmd.Parameters.Add("@MaCTHoaDon", OleDbType.Integer);
                            cmd.Parameters.Add("@MaDiaDiem", OleDbType.Integer);
                            cmd.Parameters.Add("@LoaiHang", OleDbType.Integer);
                            cmd.Parameters.Add("@SoLuong", OleDbType.Double);
                            cmd.Parameters.Add("@MaTrangThai", OleDbType.Integer);

                            cmd.Parameters["@MaHoaDonGH"].Value = maHoaDonGH;
                            cmd.Parameters["@MaCTHoaDon"].Value = chiTiet.MaChiTietHoaDon;
                            cmd.Parameters["@MaDiaDiem"].Value = chiTiet.MaDiaDiem;
                            cmd.Parameters["@LoaiHang"].Value = chiTiet.MaLoaiHang;
                            cmd.Parameters["@SoLuong"].Value = chiTiet.SoLuong;
                            cmd.Parameters["@MaTrangThai"].Value = chiTiet.MaTrangThai;

                            result = cmd.ExecuteNonQuery();

                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            if (cnn.State == ConnectionState.Open)
                            {
                                cnn.Close();
                            }
                            throw ex;
                        }

                    }
                    transaction.Commit();
                    cnn.Close();
                }
            }
        }
    }
}
